<template>
  <section class="login-forget">
    <img src="../assets/img/login.jpg" class="login-img" height="100%" />
    <div class="form-box">
      <RouterView />
    </div>
  </section>
</template>

<script>
import _ from 'lodash';
import Cookie from 'js-cookie';
import {http, catchAppError} from '@/util';
import url from '@/config';

export default {
  name: 'Login',
  data() {
    return {
      code: '',
      rememberPassword: false,
      form: {
        username: 'admin709',
        password: '123456',
        verify: ''
      }
    };
  },
  watch: {
    rememberPassword(isRemember) {
      if (!isRemember) window.localStorage.removeItem('password');
    }
  },
  beforeMount() {
    try {
      const auth = JSON.parse(window.localStorage.getItem('password'));
      this.form = {
        ...this.form,
        ...auth
      };
      this.rememberPassword = !!auth;
      // eslint-disable-next-line no-empty
    } catch (e) {}
  },
  methods: {
    verfiyCode(code) {
      this.code = code;
    },
    login() {
      const allIsCheck =
        this.form.username && this.form.password && this.form.verify;
      if (!allIsCheck) {
        this.$message({
          message: '选项不能为空',
          duration: 5000,
          type: 'error'
        });
        return;
      }
      if (this.form.verify !== this.code) {
        this.$message({
          message: '验证码不正确',
          duration: 5000,
          type: 'error'
        });
        return;
      }
      if (this.rememberPassword) {
        window.localStorage.setItem(
          'password',
          JSON.stringify(_.pick(this.form, 'username', 'password'))
        );
      }
      http
        .post(url.login, {
          username: this.form.username,
          password: this.form.password
        })
        .then(res => {
          Cookie.set('token', res.token);
          this.$router.push('/');
        })
        .catch(catchAppError);
    },
    forget() {
      window.location.href = '/forget';
    }
  }
};
</script>

<style lang="scss">
.login-forget {
  height: 100%;
  vertical-align: top;
  overflow: hidden;
  position: relative;
  .login-img {
    width: 93.5vh;
  }
  .form-box {
    position: absolute;
    left: 93.5vh;
    right: 0;
    top: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    .el-input__inner {
      height: 44px;
      line-height: 44px;
      border-radius: 0;
      border-color: #b3b3b3;
    }
    .hello {
      font-weight: bold;
      font-size: 30px;
    }
    .login-form,
    .hello,
    .intro {
      width: 320px;
      margin-left: 70px;
    }
    .login-form {
      position: relative;
      .submit-button {
        width: 166px;
        height: 50px;
        margin-top: 30px;
        background-color: #4b90ff;
        color: #fff;
      }
      .el-form-item__label {
        float: none;
        text-align: left;
      }
      .el-form-item__content {
        margin-left: 0 !important;
      }
      .verify {
        .input-verify {
          width: 227px;
          border-right: 0;
        }
        .el-button {
          display: inline-block;
          height: 44px;
          vertical-align: top;
          border: 1px solid #b3b3b3;
          border-left: 0;
          border-radius: 0;
        }
      }
    }
  }
}
</style>
